package GUILayer;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;

import CtrLayer.SaleCtr;
import ModelLayer.Employee;
import ModelLayer.PartSale;
import ModelLayer.Sale;

/**
 * BottledBeerGUI
 * this class creates the bottledBeerGUIPanel which contains a selection of bottled beers.
 *  @author Lindgren
 */
public class BottledBeerGUI {

	private JPanel bottledBeerGUIPanel;
	private Employee employee;
	private String priceType = "defaultPrice";
	private JComboBox comboBox;
	private JTextArea salesList;
	private JTextField total;

	/**
	 * Create the BottledBeerGUI.
	 * @param employee , the employee that 'owns' this GUI.
	 */
	public BottledBeerGUI(Employee employee) {
		this.employee = employee; 
		initialize();
		System.out.println("DraughtBeerGUI initialized!");
	}

	/**
	 * Initialize the bottledBeerPanel.
	 */
	public void initialize() {

		System.out.println("DraughtBeerGUI initializing");

		bottledBeerGUIPanel = new JPanel();
		bottledBeerGUIPanel.setToolTipText("bottledBeerGUIPanel");
		bottledBeerGUIPanel.setForeground(Color.BLACK);
		bottledBeerGUIPanel.setName(employee.getLogin());

		MainFrame.getBottledBeerGUIPanels().put(employee.getLogin(), bottledBeerGUIPanel);
		System.out.println("BottledBeerGUI bottledBeerGUIPanel mapped: "+MainFrame.getDraughtBeerGUIPanels().get(employee.getLogin()));
		bottledBeerGUIPanel.setLayout(new GridLayout(0, 1, 0, 0));

		Box horizontalBox = Box.createHorizontalBox();
		bottledBeerGUIPanel.add(horizontalBox);

		JPanel panel = new JPanel();
		horizontalBox.add(panel);
		panel.setToolTipText("bottledBeerGUIPanel");
		panel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_8 = new JPanel();
		panel.add(panel_8);
		panel_8.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnBudweiser = new JButton("Budweiser");
		btnBudweiser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addProduct(19);
			}
		});
		panel_8.add(btnBudweiser);

		JButton btnCorona = new JButton("Corona");
		btnCorona.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addProduct(20);
			}
		});
		panel_8.add(btnCorona);

		JButton btnVictoriaBitter = new JButton("Victoria Bitter");
		btnVictoriaBitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(21);
			}
		});
		panel_8.add(btnVictoriaBitter);

		JPanel panel_7 = new JPanel();
		panel.add(panel_7);
		panel_7.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnHeineken = new JButton("Heineken");
		btnHeineken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(22);
			}
		});
		panel_7.add(btnHeineken);

		JButton btnCrownLager = new JButton("Crown Lager");
		btnCrownLager.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(23);
			}
		});
		panel_7.add(btnCrownLager);

		JButton btnBrooklynBrownAle = new JButton("Brooklyn Brown Ale");
		btnBrooklynBrownAle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(24);
			}
		});
		panel_7.add(btnBrooklynBrownAle);

		JPanel panel_6 = new JPanel();
		panel.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnLeffeBrun = new JButton("Leffe Brun");
		btnLeffeBrun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(25);
			}
		});
		panel_6.add(btnLeffeBrun);

		JButton btnOharasCelticStout = new JButton("Oharas Celtic Stout");
		btnOharasCelticStout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(26);
			}
		});
		panel_6.add(btnOharasCelticStout);

		JButton btnOharasIrishRed = new JButton("Oharas Irish Red");
		btnOharasIrishRed.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(27);
			}
		});
		panel_6.add(btnOharasIrishRed);

		JPanel panel_5 = new JPanel();
		panel.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnWestmalle = new JButton("Westmalle");
		btnWestmalle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(28);
			}
		});
		panel_5.add(btnWestmalle);

		JButton btnChimayBlue = new JButton("Chimay Blue");
		btnChimayBlue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(29);
			}
		});
		panel_5.add(btnChimayBlue);

		JButton btnDuvel = new JButton("Duvel");
		btnDuvel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(30);
			}
		});
		panel_5.add(btnDuvel);

		JPanel panel_4 = new JPanel();
		panel.add(panel_4);
		panel_4.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnImperialStout = new JButton("Imperial Stout");
		btnImperialStout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(31);
			}
		});
		panel_4.add(btnImperialStout);

		JButton btnBitterNTwisted = new JButton("Bitter n Twisted");
		btnBitterNTwisted.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(32);
			}
		});
		panel_4.add(btnBitterNTwisted);

		JButton btnHoneyDew = new JButton("Honey Dew");
		btnHoneyDew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(33);
			}
		});
		panel_4.add(btnHoneyDew);

		JPanel panel_3 = new JPanel();
		panel.add(panel_3);
		panel_3.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnLondonPorter = new JButton("London Porter");
		btnLondonPorter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(34);
			}
		});
		panel_3.add(btnLondonPorter);

		JButton btnLondonPride = new JButton("London Pride");
		btnLondonPride.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(35);
			}
		});
		panel_3.add(btnLondonPride);

		JButton btnOhanlonsPortStout = new JButton("Ohanlons Port Stout");
		btnOhanlonsPortStout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(36);
			}
		});
		panel_3.add(btnOhanlonsPortStout);

		JPanel panel_13 = new JPanel();
		panel.add(panel_13);
		panel_13.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnGoldenChampion = new JButton("Golden Champion");
		btnGoldenChampion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(37);
			}
		});
		panel_13.add(btnGoldenChampion);

		JButton btnOldSpeckledHen = new JButton("Old Speckled Hen");
		btnOldSpeckledHen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(38);
			}
		});
		panel_13.add(btnOldSpeckledHen);

		JButton btnBbBohemianDark = new JButton("BB Bohemian Dark");
		btnBbBohemianDark.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(39);
			}
		});
		panel_13.add(btnBbBohemianDark);

		JPanel panel_15 = new JPanel();
		panel.add(panel_15);
		panel_15.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnBbBohemianLight = new JButton("BB Bohemian Light");
		btnBbBohemianLight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(40);
			}
		});
		panel_15.add(btnBbBohemianLight);

		JButton btnBishopsFinger = new JButton("Bishops Finger");
		btnBishopsFinger.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(41);
			}
		});
		panel_15.add(btnBishopsFinger);

		JButton btnStAndrewsAle = new JButton("St Andrews Ale");
		btnStAndrewsAle.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(42);
			}
		});
		panel_15.add(btnStAndrewsAle);

		JPanel panel_16 = new JPanel();
		panel.add(panel_16);
		panel_16.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnRobertBurns = new JButton("Robert Burns");
		btnRobertBurns.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(43);
			}
		});
		panel_16.add(btnRobertBurns);

		JButton btnShepherdNeame = new JButton("1698 Shepherd Neame");
		btnShepherdNeame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(44);
			}
		});
		panel_16.add(btnShepherdNeame);

		JButton btnErdingerWeiss = new JButton("Erdinger Weiss");
		btnErdingerWeiss.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(45);
			}
		});
		panel_16.add(btnErdingerWeiss);

		JPanel panel_17 = new JPanel();
		panel.add(panel_17);
		panel_17.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnErdingerDunkel = new JButton("Erdinger Dunkel");
		btnErdingerDunkel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(46);
			}
		});
		panel_17.add(btnErdingerDunkel);

		JButton btnStrongSuffolk = new JButton("Strong Suffolk");
		btnStrongSuffolk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(47);
			}
		});
		panel_17.add(btnStrongSuffolk);

		JButton btnFiddlersElbow = new JButton("Fiddlers Elbow");
		btnFiddlersElbow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(48);
			}
		});
		panel_17.add(btnFiddlersElbow);

		JPanel panel_18 = new JPanel();
		panel.add(panel_18);
		panel_18.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnHobgoblin = new JButton("Hobgoblin");
		btnHobgoblin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(49);
			}
		});
		panel_18.add(btnHobgoblin);

		JButton btnGrolsch = new JButton("Grolsch");
		btnGrolsch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(50);
			}
		});
		panel_18.add(btnGrolsch);

		JButton btnMagners = new JButton("Magners");
		btnMagners.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(51);
			}
		});
		panel_18.add(btnMagners);

		JPanel panel_19 = new JPanel();
		panel.add(panel_19);
		panel_19.setLayout(new GridLayout(0, 3, 0, 0));

		JButton btnDoubleChocolate = new JButton("Double Chocolate");
		btnDoubleChocolate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(52);
			}
		});
		panel_19.add(btnDoubleChocolate);

		JButton btnHeinekenLiter = new JButton("Heineken 5 liter");
		btnHeinekenLiter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProduct(53);
			}
		});
		panel_19.add(btnHeinekenLiter);

		JButton button = new JButton("");
		panel_19.add(button);

		JPanel panel_1 = new JPanel();
		horizontalBox.add(panel_1);
		panel_1.setLayout(null);

		JPanel VIPPanel = new JPanel();
		VIPPanel.setBounds(10, 5, 385, 27);
		panel_1.add(VIPPanel);
		VIPPanel.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_11 = new JPanel();
		VIPPanel.add(panel_11);
		panel_11.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblAmount = new JLabel("Amount");
		lblAmount.setHorizontalAlignment(SwingConstants.CENTER);
		panel_11.add(lblAmount);

		Component glue_2 = Box.createGlue();
		panel_11.add(glue_2);

		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "5", "6", "7", "8"}));
		panel_11.add(comboBox);

		JPanel SalesListPanel = new JPanel();
		SalesListPanel.setBounds(10, 43, 385, 285);
		panel_1.add(SalesListPanel);
		SalesListPanel.setBorder(new TitledBorder(null, "Sales List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		SalesListPanel.setLayout(new GridLayout(0, 1, 0, 0));

		salesList = new JTextArea();
		salesList.setEditable(false);
		SalesListPanel.add(salesList);

		JPanel panel_12 = new JPanel();
		bottledBeerGUIPanel.add(panel_12);
		panel_12.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel panel_20 = new JPanel();
		panel_12.add(panel_20);
		panel_20.setLayout(null);

		JLabel lblTotal = new JLabel("Total:");
		lblTotal.setBounds(1033, 8, 51, 14);
		panel_20.add(lblTotal);

		total = new JTextField();
		total.setBounds(1094, 5, 111, 20);
		total.setBackground(Color.WHITE);
		total.setEditable(false);
		panel_20.add(total);
		total.setColumns(10);

		JLabel lblKr = new JLabel("Kr");
		lblKr.setBounds(1215, 8, 37, 14);
		panel_20.add(lblKr);

		JPanel panel_2 = new JPanel();
		panel_12.add(panel_2);
		panel_2.setLayout(new GridLayout(0, 4, 0, 0));

		JButton btnBack_1 = new JButton("Back");
		panel_2.add(btnBack_1);
		btnBack_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				back();
			}
		});
		btnBack_1.setBackground(Color.YELLOW);

		JButton btnRemovePartSale = new JButton("Minus");
		btnRemovePartSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removePartSale();
				updateSalesList();
			}
		});
		panel_2.add(btnRemovePartSale);
		btnRemovePartSale.setForeground(Color.WHITE);
		btnRemovePartSale.setBackground(Color.RED);

		Component glue_3 = Box.createGlue();
		panel_2.add(glue_3);

		JButton btnGoToPayment_1 = new JButton("Go To Payment");
		panel_2.add(btnGoToPayment_1);
		btnGoToPayment_1.setForeground(Color.WHITE);
		btnGoToPayment_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				endSale();
				openPaymentGUI();
			}
		});
		btnGoToPayment_1.setBackground(Color.GREEN);

		JPanel panel_14 = new JPanel();
		panel_12.add(panel_14);
	}

	/**
	 * Removes the current panel (GUI) and sets the paymentPanel instead.
	 */
	private void openPaymentGUI()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getBottledBeerGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getPaymentGUIPanels().get(employee.getLogin()));
			MainFrame.getPaymentGUIPanels().get(employee.getLogin()).repaint();
			MainFrame.getPaymentGUIs().get(employee.getLogin()).updateSalesList();
		} catch (Exception e) {
			System.err.println("opening paymentGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Calculates the total price on the sale using the salecontroller.
	 */
	private void endSale()
	{
		try {
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			SaleCtr.getInstance().endSale(sale);
		} catch (Exception e) {
			System.err.println("Calculating total price failed");
			e.printStackTrace();
		}
	}

	/**
	 * @return the priceType
	 */
	public String getPriceType() {
		return priceType;
	}

	/**
	 * @param priceType the priceType to set
	 */
	public void setPriceType(String priceType) {
		this.priceType = priceType;
	}

	/**
	 * Updates the sales list JTextArea
	 */
	public void updateSalesList()
	{
		try {
			salesList.setText("");
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			ArrayList<PartSale> partSales = sale.getPartsales();
			for(PartSale partSale : partSales)
			{
				salesList.insert((partSale.getAmount() + " x " + partSale.getProduct().getName() + " : " + partSale.getPrice() + " kr. \n"), 0);
			}
			SaleCtr saleCtr = SaleCtr.getInstance();
			saleCtr.endSale(sale);
			total.setText(String.valueOf(sale.getTotal()));
		} catch (Exception e) {
			System.err.println("update saleslist failed");
			e.printStackTrace();
		}
	}

	/**
	 * Return the user to saleGUI.
	 */
	private void back()
	{
		try {
			MainFrame.getUserFramePanels().get(employee.getLogin()).remove(MainFrame.getBottledBeerGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).add(MainFrame.getSaleGUIPanels().get(employee.getLogin()));
			MainFrame.getUserFramePanels().get(employee.getLogin()).repaint();
			MainFrame.getSaleGUIs().get(employee.getLogin()).updateSalesList();
		} catch (Exception e) {
			System.err.println("Return to saleGUI failed");
			e.printStackTrace();
		}
	}

	/**
	 * Adds a product to a partSale and add that partSale to the current sale using the sale controller.
	 * Also resets all special price buttons (staff, halfPint, VIP1 and VIP2) and resets priceType to default.
	 * @param productID
	 */
	private void addProduct(int productID)
	{
		try {
			Sale sale = MainFrame.getSaleGUIs().get(employee.getLogin()).getSale();
			SaleCtr.getInstance().addProduct(productID, comboBox.getSelectedIndex()+1, priceType, sale);
			updateSalesList();
			comboBox.setSelectedIndex(0);
			priceType = "defaultPrice";
		} catch (Exception e) {
			System.err.println("add product failed");
			e.printStackTrace();
		}
	}

	/**
	 * Removes the last partSale from the sale 
	 */
	private void removePartSale()
	{
		try {
			SaleCtr.getInstance().removePartSale( MainFrame.getSaleGUIs().get(employee.getLogin()).getSale() );
		} catch (Exception e) {
			System.err.println("Removing last partsale from sale failed");
			e.printStackTrace();
		}
	}
}
